/*
 * Copyright (c) 2000,2002-2005 Silicon Graphics, Inc.
 * All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
#ifndef __DFS_BMAP_BTREE_H__
#define __DFS_BMAP_BTREE_H__

struct dfs_btree_cur;
struct dfs_btree_block;
struct dfs_mount;
struct dfs_inode;
struct dfs_trans;

/*
 * Extent state and extent format macros.
 */
#define DFS_EXTFMT_INODE(x)	\
	(dfs_sb_version_hasextflgbit(&((x)->i_mount->m_sb)) ? \
		DFS_EXTFMT_HASSTATE : DFS_EXTFMT_NOSTATE)
#define ISUNWRITTEN(x)	((x)->br_state == DFS_EXT_UNWRITTEN)

/*
 * Btree block header size depends on a superblock flag.
 */
#define DFS_BMBT_BLOCK_LEN(mp) \
	(dfs_sb_version_hascrc(&((mp)->m_sb)) ? \
		DFS_BTREE_LBLOCK_CRC_LEN : DFS_BTREE_LBLOCK_LEN)

#define DFS_BMBT_REC_ADDR(mp, block, index) \
	((dfs_bmbt_rec_t *) \
		((char *)(block) + \
		 DFS_BMBT_BLOCK_LEN(mp) + \
		 ((index) - 1) * sizeof(dfs_bmbt_rec_t)))

#define DFS_BMBT_KEY_ADDR(mp, block, index) \
	((dfs_bmbt_key_t *) \
		((char *)(block) + \
		 DFS_BMBT_BLOCK_LEN(mp) + \
		 ((index) - 1) * sizeof(dfs_bmbt_key_t)))

#define DFS_BMBT_PTR_ADDR(mp, block, index, maxrecs) \
	((dfs_bmbt_ptr_t *) \
		((char *)(block) + \
		 DFS_BMBT_BLOCK_LEN(mp) + \
		 (maxrecs) * sizeof(dfs_bmbt_key_t) + \
		 ((index) - 1) * sizeof(dfs_bmbt_ptr_t)))

#define DFS_BMDR_REC_ADDR(block, index) \
	((dfs_bmdr_rec_t *) \
		((char *)(block) + \
		 sizeof(struct dfs_bmdr_block) + \
	         ((index) - 1) * sizeof(dfs_bmdr_rec_t)))

#define DFS_BMDR_KEY_ADDR(block, index) \
	((dfs_bmdr_key_t *) \
		((char *)(block) + \
		 sizeof(struct dfs_bmdr_block) + \
		 ((index) - 1) * sizeof(dfs_bmdr_key_t)))

#define DFS_BMDR_PTR_ADDR(block, index, maxrecs) \
	((dfs_bmdr_ptr_t *) \
		((char *)(block) + \
		 sizeof(struct dfs_bmdr_block) + \
		 (maxrecs) * sizeof(dfs_bmdr_key_t) + \
		 ((index) - 1) * sizeof(dfs_bmdr_ptr_t)))

/*
 * These are to be used when we know the size of the block and
 * we don't have a cursor.
 */
#define DFS_BMAP_BROOT_PTR_ADDR(mp, bb, i, sz) \
	DFS_BMBT_PTR_ADDR(mp, bb, i, dfs_bmbt_maxrecs(mp, sz, 0))

#define DFS_BMAP_BROOT_SPACE_CALC(mp, nrecs) \
	(int)(DFS_BMBT_BLOCK_LEN(mp) + \
	       ((nrecs) * (sizeof(dfs_bmbt_key_t) + sizeof(dfs_bmbt_ptr_t))))

#define DFS_BMAP_BROOT_SPACE(mp, bb) \
	(DFS_BMAP_BROOT_SPACE_CALC(mp, be16_to_cpu((bb)->bb_numrecs)))
#define DFS_BMDR_SPACE_CALC(nrecs) \
	(int)(sizeof(dfs_bmdr_block_t) + \
	       ((nrecs) * (sizeof(dfs_bmbt_key_t) + sizeof(dfs_bmbt_ptr_t))))
#define DFS_BMAP_BMDR_SPACE(bb) \
	(DFS_BMDR_SPACE_CALC(be16_to_cpu((bb)->bb_numrecs)))

/*
 * Maximum number of bmap btree levels.
 */
#define DFS_BM_MAXLEVELS(mp,w)		((mp)->m_bm_maxlevels[(w)])

/*
 * Prototypes for dfs_bmap.c to call.
 */
extern void dfs_bmdr_to_bmbt(struct dfs_inode *, dfs_bmdr_block_t *, int,
			struct dfs_btree_block *, int);
extern void dfs_bmbt_get_all(dfs_bmbt_rec_host_t *r, dfs_bmbt_irec_t *s);
extern dfs_filblks_t dfs_bmbt_get_blockcount(dfs_bmbt_rec_host_t *r);
extern dfs_fsblock_t dfs_bmbt_get_startblock(dfs_bmbt_rec_host_t *r);
extern dfs_fileoff_t dfs_bmbt_get_startoff(dfs_bmbt_rec_host_t *r);
extern dfs_exntst_t dfs_bmbt_get_state(dfs_bmbt_rec_host_t *r);

extern dfs_filblks_t dfs_bmbt_disk_get_blockcount(dfs_bmbt_rec_t *r);
extern dfs_fileoff_t dfs_bmbt_disk_get_startoff(dfs_bmbt_rec_t *r);

extern void dfs_bmbt_set_all(dfs_bmbt_rec_host_t *r, dfs_bmbt_irec_t *s);
extern void dfs_bmbt_set_allf(dfs_bmbt_rec_host_t *r, dfs_fileoff_t o,
			dfs_fsblock_t b, dfs_filblks_t c, dfs_exntst_t v);
extern void dfs_bmbt_set_blockcount(dfs_bmbt_rec_host_t *r, dfs_filblks_t v);
extern void dfs_bmbt_set_startblock(dfs_bmbt_rec_host_t *r, dfs_fsblock_t v);
extern void dfs_bmbt_set_startoff(dfs_bmbt_rec_host_t *r, dfs_fileoff_t v);
extern void dfs_bmbt_set_state(dfs_bmbt_rec_host_t *r, dfs_exntst_t v);

extern void dfs_bmbt_disk_set_allf(dfs_bmbt_rec_t *r, dfs_fileoff_t o,
			dfs_fsblock_t b, dfs_filblks_t c, dfs_exntst_t v);

extern void dfs_bmbt_to_bmdr(struct dfs_mount *, struct dfs_btree_block *, int,
			dfs_bmdr_block_t *, int);

extern int dfs_bmbt_get_maxrecs(struct dfs_btree_cur *, int level);
extern int dfs_bmdr_maxrecs(struct dfs_mount *, int blocklen, int leaf);
extern int dfs_bmbt_maxrecs(struct dfs_mount *, int blocklen, int leaf);

extern int dfs_bmbt_change_owner(struct dfs_trans *tp, struct dfs_inode *ip,
				 int whichfork, dfs_ino_t new_owner,
				 struct list_head *buffer_list);

extern struct dfs_btree_cur *dfs_bmbt_init_cursor(struct dfs_mount *,
		struct dfs_trans *, struct dfs_inode *, int);

#endif	/* __DFS_BMAP_BTREE_H__ */
